#include "task_sensor_dect.h"


static TaskHandle_t sensor_detect_task_handle;

QueueHandle_t sensor_dect_queue;

static u8 Sensor_Dectect_Act_Buf[NUM_DRV_ACT] = {0,KEY_STASHORT,KEY_STALONG_5S};


//初始化传感器通讯队列
void sensor_dectect_val_init()
{
    sensor_dect_queue = xQueueCreate(5,sizeof(key_act_param));
}


EM_DRV_ACT sensor_dectect_get_act(u8 actx)
{
	int i=0;

	for(i=DRV_ACT_KEY_START;i<DRV_ACT_KEY_END;i++)
	{
		if(Sensor_Dectect_Act_Buf[i] == actx) return i;
	}
    return DRV_ACT_NONE;
}

static void sensor_dectect_loop(void *pvParameters)
{

    ENUM_KEYSTA keySta = KEY_STANONE; //按键没有按下
    EM_DRV_ACT drvAct = DRV_ACT_NONE; //按键没用动作


    TickType_t xLastWakeTime; //精准延时
    key_act_param act_param;
    sensor_dectect_val_init(); //初始化传感器通讯队列

    key_init(); //按键初始化
    LOG_INFO("enter task-> sensor_dectect\n");

    while (1)
    {
       xLastWakeTime = xTaskGetTickCount();
       key_update_sta();
       
        for(int i=0;i<NUM_KEY;i++)
        {
            keySta = key_get_state(i);
            key_set_state(i,KEY_STANONE);//恢复初始状态
            
		    if(keySta != KEY_STANONE)
            {
                drvAct = sensor_dectect_get_act((u8)keySta);   //状态判断器
                if(drvAct != DRV_ACT_NONE)
			    {
                    act_param.drvAct = drvAct;
                    act_param.key_num = i;
                    xQueueSend(sensor_dect_queue,&act_param,0);    //队列发送数据
	                LOG_INFO("sensor_send_sensor_msg() send driver act %d,key is %d\n",act_param.drvAct,act_param.key_num);
				    drvAct = DRV_ACT_NONE;
                }
            }
        }
        vTaskDelayUntil(&xLastWakeTime, ( TASK_SENSOR_DECT_PERIOD / portTICK_PERIOD_MS )); //10S扫描一次
    }
}


void task_sensor_dectect_init()
{
    xTaskCreate(sensor_dectect_loop,"task_sensor_dectect",4096,NULL,TASK_PROP_DRIVER,&sensor_detect_task_handle);
}